/**
 * ECP Web开发通用枚举选择控件.
 * Copyright(c) 2012-2020 YGSoft.Inc
 * @author zhengxianwei 
 * @date 2016-04-13
 */
define(["jquery","ecp.service","ecp.utils","ecp.component.entityselect","ecp.utils.i18n"],function($,ecp,utils,$entityselect,i18n){
	/**
	 * 声明BisComboBox对象.
	 * @param {Object} control 控件的jQuery对象，即占位符。
	 * @param {Object} option 控件的属性对象
	 */
	var BisEntitySelect = function(control,option){
		if(!control || control.length === 0){return;}
		//初始化
		this.init(control,option);
		//渲染控件
		this._render();
	}
	/**
	 * 通过原型定义相关方法，比如渲染、赋值等等.
	 */
	BisEntitySelect.prototype = {
			/**
			 * 初始化.
			 * @param {Object} control 控件的jQuery对象，即占位符。
	    	 * @param {Object} option 控件的属性对象
			 */
			init: function(control, option){
				this.control = control;
				var dOpt = this.control.attr("data-option");
				if(dOpt && dOpt.length !== 0){
					dOpt = $.parseJSON(dOpt);
				}else{
					dOpt = {};
				}
				this.option = $.extend(true,{},option,dOpt);
			    //下拉数据回调函数
				delete this.option.data;
				this.option.listDatas = $.proxy(this._getListDatas,this);
				this.option.entityGridData = $.proxy(this._getEntityGridData,this);
				if(this.option.showTree){
					this.option.entityTreeData = $.proxy(this._getEntityTreeData,this);
					this.option.treeSelectNodeFn = $.proxy(this._getTreeSelectNodeFn,this);
					//树的懒加载回调函数
					this.option.treeLazyDatas = $.proxy(this._getTreeLazyDatas,this);
				}
			},
			
			/**
	    	 * 渲染.
	    	 */
			_render: function(){
				this.entityselect = this.control.entityselect(this.option);
				this.entityselect.base(this);
	    	},
	    	
	    	/**
	    	 * 获得实体选择控件对象.
	    	 */
	    	getEntitySelect: function(){
	    		return this.entityselect;
	    	},
	    	
	    	/**
	    	 * 获得comboBox对象.
	    	 */
	    	getComboBox: function(){
	    		return this.entityselect.getComboBox();
	    	},
	    	
	    	/**
	    	 * 继承comboBox表格中渲染须实现的方法.
	    	 * @param {Object} obj 控件对象
	    	 */
	    	inheritGridMethod: function(obj){
	    		if(!obj){return;}
	    		this.entityselect.inheritGridMethod(obj);
	    	},
	    	/**
	    	 * 获得下拉列表数据.
	    	 * @param {Function} callback 回调
	    	 * @param {Object} option 控件的属性对象
	    	 * @param {Object} filter 过滤对象
	    	 */
	    	_getListDatas: function(callback,option,filter){
	    		this._getServer(callback,option,filter,true);
	    	},
	    	
	    	/**
	    	 * 获得实体表格数据.
	    	 * @param {Function} callback 回调
	    	 * @param {Object} option 控件的属性对象
	    	 * @param {Object} filter 过滤对象
	    	 */
	    	_getEntityGridData: function(callback,option,filter){
	    		this._getServer(callback,option.entityGrid,filter);
	    	},
	    	
	    	/**
	    	 * 获得树数据.
	    	 * @param {Function} callback 回调
	    	 * @param {Object} option 控件的属性对象
	    	 */
	    	_getEntityTreeData: function(option,callback){
	    		this._getServer(callback,option.entityTree);
	    	},
	    	
	    	/**
	    	 * 获得树懒加载数据.
	    	 * @param {Function} callback 回调
	    	 * @param {Object} option 控件的属性对象
	    	 */
	    	_getTreeLazyDatas: function(option,callback,filter){
	    		this._getServer(callback, option.treeLazyOptions, filter);
	    	},
	    	
	    	/**
	    	 * 获得服务.
	    	 * @param {Function} callback 回调
	    	 * @param {Object} option 控件的属性对象
	    	 * @param {Object} filter 过滤对象
	    	 */
	    	_getServer: function( callback,option,filter, isPageSize){
	    		if(!option){return;}
	    		var beanId = option.beanId,
	    			method = option.method,
	    			needContext = option.needContext,
	    			pageSize = option.pageSize,
					requestTyle = option.requestType,
	    			param = option.param;
	    		if(!beanId || !method){
	    			window.console.error(i18n.select.paramNotmatch);
	    			return;
	    		}
				if(!requestTyle){
					requestTyle = "GET";
				}
	    		if(!param){
					param = [];
				}
	    		var p = [];
	    		if(needContext !== false){
	    			p.push(ecp.DataContextUtil.getEcpDataContext());
	    		}
	    		if(isPageSize === true){
	    			if(filter && filter.pageSize){
	    				filter.pageSize = pageSize;
	    			}
	    			p.push(filter);
	    		}else if(filter){
	    			p.push(filter);
	    		}
    			param = p.concat(param);
				//获取所有菜单
				var reqMathod = requestTyle == "GET" ? "doGetAsync" : "doPostAsync";
				ecp.RemoteService[reqMathod](beanId,method,param,function(result){
					if(result.isSuccess()){
		            	callback(result.data);
					}else{
						window.console.error("beanId: "+beanId +" method: " +method +" \t\n" + i18n.select.queryError+": "+result.data);
					}
	    		});
	    	},
	    	
	    	/**
	    	 * 左树右表情况的联动处理
	    	 * @param {Object} option对象
	    	 */
	    	_getTreeSelectNodeFn: function(option){
	    		if(!option || !option.entity){return;}
	    		var entity = option.entity,
	    			opt = entity.option,
	    			grid = option.entityGridObj,
	    			tree = option.entityTreeObj,
	    			node = option.data && option.data.node,
	    			filter = option.entity.getFieldFilter(),
	    			callback = function(result){
		    			grid.value(result);
		    		},
	    			pageSize = grid.getOption().pageSize;
	    		if(pageSize){
	    			filter.pageSize = pageSize;
	    		}
	    		if(node){
	    			filter.node = node.original;
	    		}
	    		filter.entityTree = opt.entityTree;
	    		this._getServer(callback,opt.entityGrid,filter);
	    	}
	};
	
	(function($) {
		var old = $.fn.bisEntitySelect;
		$.fn.bisEntitySelect = function(option){
			var me = this;
			if(!me || me.length === 0){
				if(window.console){
					window.console.error("ecp.component.bisEntitySelect"+i18n.select.renderError);
				}
				return;
			}
			option = option || {};
			var type = typeof(option);
			//如果第一个参数传入的是对象或空则初始化空件，如果传入的是字符串，则认为是调用里面的内部方法。
			if (type === "object") {
				var bess = [];
				this.each(function() {
					var instanceOptions = $.extend({}, option, true);
					var instance = new BisEntitySelect($(this), instanceOptions);
					this.bisEntitySelect = instance;
					bess.push(instance);
				});
				if(bess.length === 1){
					bess = bess[0];
				}
				return bess;
			} else if (type === "string") {
				var args = Array.prototype.slice.call(arguments, 1);
				if (BisEntitySelect.prototype[option]) {
					return BisEntitySelect.prototype[option].call(args);
				}
			} else if (option === true) {
				return me[0] && me[0].bisEntitySelect;
			}
		};
		$.fn.bisEntitySelect.Constructor = BisEntitySelect;
		$.fn.bisEntitySelect.noConflict = function() {
			$.fn.bisEntitySelect = old;
			return this;
		};
	}(jQuery));
	return BisEntitySelect;
});